using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthBarUI : MonoBehaviour
{
    public GameObject healthUIPrefeb;

    public Transform barPoint;
    
    public bool alwaysVisible;

    public float visibleTime;

    private float timeLeft;

    private int pooledAmount = 100;

    Image healthSlider;
    Transform UIbar;
    Transform cam;

    CharacterStats currentStats;

    private void Awake()
    {
        currentStats = GetComponent<CharacterStats>();

        currentStats.updateHealthBarOnAttack += updateHealthBar;
    }
    

    void OnEnable()
    {
        cam = Camera.main.transform;
        foreach (Canvas canvas in FindObjectsOfType<Canvas>())
        {
            if (canvas.renderMode == RenderMode.WorldSpace)
            {
                UIbar=Instantiate(healthUIPrefeb, canvas.transform).transform;
                healthSlider = UIbar.GetChild(0).GetComponent<Image>();

                UIbar.gameObject.SetActive(alwaysVisible); 
            }
        }
    }

    private void updateHealthBar(int currentHealth, int maxHealth)
    {
        if (currentHealth <= 0)
        {
            Destroy(UIbar.gameObject);
        }
        timeLeft = visibleTime;
        UIbar.gameObject.SetActive(true);
        float silderPercent = (float)currentHealth / maxHealth;
        healthSlider.fillAmount = silderPercent;
    }
    private void LateUpdate()
    {
        if (UIbar != null)
        {
            UIbar.position = barPoint.position;
            UIbar.forward = -cam.forward;
            if (timeLeft <= 0&&!alwaysVisible)
            {
                UIbar.gameObject.SetActive(false);
            }
            else
            {
                timeLeft -= Time.deltaTime;
            }
        }
    }
}
